<template>
    <div id="app">
        <table ref="tableExcel" width="100%" border="1" cellspacing="0" cellpadding="0">
            <tr>
                <td colspan="5" align="center">喵呜单词</td>
            </tr>
            <tr>
                <td>单词</td>
                <td>英标</td>
                <td>答案</td>
            </tr>
            <tr v-for="item in data">
                <td>{{item.a}}</td>
                <td>{{item.b}}</td>
                <td>{{item.c}}</td>
            </tr>
        </table>
        <button @click="btnClick">导出</button>
    </div>
</template>

<script>
    function method5(dom, name) {
        tableToExcel(dom, name)
    }
    var tableToExcel = (function () {
        var uri = 'data:application/vnd.ms-excel;base64,',
            template =
            '<html><head><meta charset="UTF-8"></head><body><table>{table}</table></body></html>',
            base64 = function (s) {
                return window.btoa(unescape(encodeURIComponent(s)))
            },
            format = function (s, c) {
                return s.replace(/{(\w+)}/g,
                    function (m, p) {
                        return c[p];
                    })
            }
        return function (table, name) {
            var ctx = {
                worksheet: name || 'Worksheet',
                table: table.innerHTML
            }
            window.location.href = uri + base64(format(template, ctx))
        }
    })()
    import {
        data
    } from './data.js'
    export default {
        name: 'App',
        data() {
            return {
                text: data,
                data: []
            }
        },
        methods: {
            // 格式1
            handleText() {
                let text = this.text.replace(/[\r\n]/g, ($0) => {
                    return '@'
                });
                let array = text.split('@@');
                let data = [];
                array.forEach((item) => {
                    let a = /([\w ]+)\ ?\[?/.exec(item)[1];
                    let b;
                    item.replace(/\[.+\]/, ($0) => {
                        b = $0;
                    })
                    let c1 = /常考意思\：?(.+)/.exec(item)[1];
                    let d1
                    c1 = c1.replace(/@六选二同义词：(.+)/, ($0, $1) => {
                        d1 = $1;
                        return '';
                    });
                    let carray = c1.split('@');
                    carray = carray.map((e) => {
                        return /[\u4e00-\u9fa5|……|，|⋯]+/.exec(e)[0]
                    })
                    let c = carray.join(',');
                    data.push({
                        a,
                        b,
                        c
                    })
                    if (d1) {
                        let d2 = d1.split(',');
                        d2.forEach((item) => {
                            let f
                            item = item.replace(/[\u4e00-\u9fa5|……|，|（|）|⋯]+/, ($0) => {
                                f = $0;
                                return '';
                            })
                            data.push({
                                a: item,
                                b: '',
                                c: f || c
                            })
                        })
                    }
                })
                this.data = data;
                console.log(data.length);
            },
            handleText2(){
                let text = this.text.replace(/[\r\n]/g, ($0) => {
                    return '@'
                });
                let array = text.split('@');
                let a,b;
                let data = [];
                array.forEach((item) => {
                    if(/\d+. /.test(item)) return;
                    item.replace(/\w+/,($0)=>{
                        a = $0;
                    })
                    item.replace(/[\u4e00-\u9fa5][\u4e00-\u9fa5|……|，|（|）|⋯| ]+/,($0)=>{
                        console.log($0);
                        b = $0;
                    })
                    data.push({
                        a,
                        b: '',
                        c: b
                    })
                })
                this.data = data;
            },
            btnClick() {
                method5(this.$refs.tableExcel, '喵呜单词');
            }
        },
        mounted() {
            // this.handleText();
            this.handleText2();
        }
    }

</script>

<style>
    #app {
        font-family: 'Avenir', Helvetica, Arial, sans-serif;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        text-align: center;
        color: #2c3e50;
        margin-top: 60px;
    }

</style>
